//
//  VerifyButton.swift
//  Employee
//
//  Created by swordjoy on 2017/5/2.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class VerifyButton: UIButton {
  
  var isCount: Bool = false {
    willSet {
      
      if newValue {
        
        countDownTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
        setTitleColor(GlobalConfig.TextLightColor, for: .normal)
        remainingSeconds = 60
        
      }
      else {
        
        countDownTimer?.invalidate()
        setTitle("获取验证码", for: .normal)
        setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
        
      }
      
      isEnabled = !newValue
      
    }
    
  }
  
  var remainingSeconds: Int = 0 {
    willSet {
      
      if newValue <= 0 {
        isCount = false
        return
      }
      
      setTitle("(\(newValue))秒后重试", for: .normal)

    }
    
  }
  
  fileprivate var countDownTimer: Timer?

}

// MARK: - action

extension VerifyButton {
  
  @objc fileprivate func updateTime() {
  
    remainingSeconds -= 1
  
  }
  
}
